//
//  Emoticon.swift
//  测试键盘
//
//  Created by Tanenbaum on 15/8/6.
//  Copyright © 2015年 GZJ. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {
    
    var id: String
    var groupName: String = ""
    var emoticons: [Emoticon]?
    init(id: String,groupName: String = "") {
        
        self.id = id
        self.groupName = groupName
    }
    //字典转模型
    class func package() ->[EmoticonPackage] {
        
        let path = bundlePath.stringByAppendingPathComponent("emoticons.plist")
        let dict = NSDictionary(contentsOfFile: path)!
        let array = dict["packages"]as![[String: AnyObject]]
        var arrayM = [EmoticonPackage]()
        arrayM.append(EmoticonPackage(id:"", groupName: "最近").appendEmptyButton())
        for item in array {
            let id = item["id"] as! String
            let package = EmoticonPackage(id: id).loadEmoticon().appendEmptyButton()
            arrayM.append(package)
        }
        return arrayM
    }
    
    //获取bundle路径
    private static let bundlePath = NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent("Emoticons.bundle")
    
    //加载表情函数
    func loadEmoticon() -> Self {
        
        let path = EmoticonPackage.bundlePath.stringByAppendingPathComponent(id).stringByAppendingPathComponent("info.plist")
        let dict = NSDictionary(contentsOfFile: path)!
        groupName = dict["group_name_cn"] as! String
        let array = dict["emoticons"] as! [[String: String]]
        emoticons = [Emoticon]()
       
        //遍历数组
        var index = 0
        for d in array {
            
            emoticons!.append(Emoticon(id:id,dict:d))
            
            index++
            
            if index == 20 {
                emoticons?.append(Emoticon(remove: true))
                // WARNING -千万不要把index写在外面
                index = 0
          }
       }
        return self
    }
    
    //追加空白按钮
    func appendEmptyButton() ->Self {
       
        if emoticons == nil {
            emoticons = [Emoticon]()
        }
        let count = emoticons!.count % 21
        if count > 0 || emoticons!.count == 0 {
            
            for _ in count..<20 {
                
                emoticons?.append(Emoticon(remove: false))
            }
            
            emoticons?.append(Emoticon(remove: true))
        }
        
        return self
    }
    override var description: String{
        
        return "\(id)\(groupName)\(emoticons)"
    }
}




class Emoticon: NSObject {

    var id: String?
    var chs: String?
    var png: String? 
    
    var imagePath:String {
        if png == nil {
            return " "
        }
        
        return EmoticonPackage.bundlePath.stringByAppendingPathComponent(id!).stringByAppendingPathComponent(png!)
    }
    var emoji: String?
    var code: String? {
      didSet{
       
        let scanner = NSScanner(string: code!)
        var value : UInt32 = 0
        scanner.scanHexInt(&value)
        emoji = String(Character(UnicodeScalar(value)))
        
        }
    }
    var removeEmoticon = false
    init(remove:Bool) {
        
        removeEmoticon = remove
    }
    
    init(id:String, dict:[String:String]) {
        super.init()
        self.id = id
        setValuesForKeysWithDictionary(dict)
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String{
        
        return ("\(chs)\(png)\(code)")
    }
}
